<template>
  <div class="app-container" id="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="姓名" prop="employeeId" v-if="isAdmin">
        <el-select
          v-model="queryParams.employeeId"
          placeholder="请选择员工"
          clearable
          filterable
          @change="handleQuery"
        >
          <el-option
            v-for="emp in employeeOptions"
            :key="emp.value"
            :label="emp.label"
            :value="emp.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="工作年限" prop="workYear">
        <el-input
          v-model="queryParams.workYear"
          placeholder="请输入工作年限"
          filterable clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

<!--      <el-form-item label="用户ID" prop="userId">-->
<!--        <el-input-->
<!--          v-model="queryParams.userId"-->
<!--          placeholder="请输入用户ID"-->
<!--          filterable clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="部门ID" prop="deptId">-->
<!--        <el-input-->
<!--          v-model="queryParams.deptId"-->
<!--          placeholder="请输入部门ID"-->
<!--          filterable clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <!-- 更多查询条件-->
      <template v-if="isShowMore">
        <el-form-item label="年假总时长" prop="annualLeave">
          <el-input
            v-model="queryParams.annualLeave"
            placeholder="请输入年假总时长"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="剩余年假" prop="remainingAnnualLeave">
          <el-input
            v-model="queryParams.remainingAnnualLeave"
            placeholder="请输入剩余年假"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="剩余事假" prop="remainingPersonalLeave">
          <el-input
            v-model="queryParams.remainingPersonalLeave"
            placeholder="请输入剩余事假"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="剩余病假" prop="remainingSickLeave">
          <el-input
            v-model="queryParams.remainingSickLeave"
            placeholder="请输入剩余病假"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="剩余婚假" prop="remainingMarriageLeave">
          <el-input
            v-model="queryParams.remainingMarriageLeave"
            placeholder="请输入剩余婚假"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="剩余产假" prop="remainingMaternityLeave">
          <el-input
            v-model="queryParams.remainingMaternityLeave"
            placeholder="请输入剩余产假"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="剩余陪产假" prop="remainingFaternityLeave">
          <el-input
            v-model="queryParams.remainingFaternityLeave"
            placeholder="请输入剩余陪产假"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="剩余加班假" prop="remainingOvertimeLeave">
          <el-input
            v-model="queryParams.remainingOvertimeLeave"
            placeholder="请输入剩余加班假"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="剩余其他假" prop="remainingOthersLeave">
          <el-input
            v-model="queryParams.remainingOthersLeave"
            placeholder="请输入剩余其他假"
            filterable clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>


      </template>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        <el-button :icon="isShowMore?'el-icon-d-arrow-right':'el-icon-d-arrow-left'" size="mini" type="warning" @click="isShowMore = !isShowMore">更多查询</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="primary"-->
<!--          plain-->
<!--          icon="el-icon-plus"-->
<!--          size="mini"-->
<!--          @click="handleAdd"-->
<!--          v-hasPermi="['leave:balance:add']"-->
<!--        >新增</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['leave:balance:edit']"
        >修改</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="danger"-->
<!--          plain-->
<!--          icon="el-icon-delete"-->
<!--          size="mini"-->
<!--          :disabled="multiple"-->
<!--          @click="handleDelete"-->
<!--          v-hasPermi="['leave:balance:remove']"-->
<!--        >删除</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['leave:balance:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="balanceList" @selection-change="handleSelectionChange" stripe :height="tableHeight">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" width="55" label="序号" align="center" fixed/>
<!--      <el-table-column label="编号" align="center" prop="id" />-->
      <el-table-column label="姓名" align="center">
        <template slot-scope="scope">
          {{ getEmployeeNameById(scope.row.employeeId) }}
        </template>
      </el-table-column>
      <el-table-column label="工作年限" align="center" prop="workYear" />
      <el-table-column label="年假总时长" align="center">
        <template slot-scope="scope">
          {{ formatLeaveHours(scope.row.annualLeave) }}
        </template>
      </el-table-column>
      <el-table-column label="剩余年假" align="center">
        <template slot-scope="scope">
          {{ formatLeaveHours(scope.row.remainingAnnualLeave) }}
        </template>
      </el-table-column>
      <el-table-column label="剩余事假" align="center">
        <template slot-scope="scope">
          {{ formatLeaveHours(scope.row.remainingPersonalLeave) }}
        </template>
      </el-table-column>
      <el-table-column label="剩余病假" align="center">
        <template slot-scope="scope">
          {{ formatLeaveHours(scope.row.remainingSickLeave) }}
        </template>
      </el-table-column>
      <el-table-column label="剩余婚假" align="center">
        <template slot-scope="scope">
          {{ formatLeaveHours(scope.row.remainingMarriageLeave) }}
        </template>
      </el-table-column>
      <el-table-column label="剩余产假" align="center">
        <template slot-scope="scope">
          {{ formatLeaveHours(scope.row.remainingMaternityLeave) }}
        </template>
      </el-table-column>
      <el-table-column label="剩余陪产假" align="center">
        <template slot-scope="scope">
          {{ formatLeaveHours(scope.row.remainingFaternityLeave) }}
        </template>
      </el-table-column>
      <el-table-column label="剩余加班假" align="center">
        <template slot-scope="scope">
          {{ formatLeaveHours(scope.row.remainingOvertimeLeave) }}
        </template>
      </el-table-column>
      <el-table-column label="剩余其他假" align="center">
        <template slot-scope="scope">
          {{ formatLeaveHours(scope.row.remainingOthersLeave) }}
        </template>
      </el-table-column>
<!--      <el-table-column label="用户ID" align="center" prop="userId" />-->
<!--      <el-table-column label="部门ID" align="center" prop="deptId" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="180" fixed="right">
        <template slot-scope="scope">
          <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="handleInfo(scope.row)"
              v-hasPermi="['leave:balance:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['leave:balance:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['leave:balance:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改员工假期管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="68%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row :gutter="20">
      <el-col :span="8">
        <el-form-item label="姓名" prop="employeeId">
          <el-select
            v-model="form.employeeId"
            placeholder="请选择员工"
            clearable
            filterable
          >
            <el-option
              v-for="emp in employeeOptions"
              :key="emp.value"
              :label="emp.label"
              :value="emp.value"
            />
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="工作年限" prop="workYear">
              <el-input v-model="form.workYear" placeholder="自动计算" readonly />
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="年假总时长" prop="annualLeave">
              <el-input :value="formatLeaveHours(form.annualLeave)" placeholder="自动计算" readonly />
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="剩余年假" prop="remainingAnnualLeave">
              <el-input-number
                v-model="form.remainingAnnualLeave"
                controls-position="right"
                placeholder="请输入剩余年假"
                style="width: 40%;"
              />
              <span style="margin-left: 8px; color: #606266; font-size: 15px;">
                小时 = {{ formatHoursToDays(form.remainingAnnualLeave) }}
              </span>
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="剩余事假" prop="remainingPersonalLeave">
              <el-input-number
                v-model="form.remainingPersonalLeave"
                placeholder="请输入剩余事假"
                controls-position="right"
                style="width: 40%;"
              />
              <span style="margin-left: 8px; color: #606266; font-size: 15px;">
                小时 = {{ formatHoursToDays(form.remainingPersonalLeave) }}
              </span>
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="剩余病假" prop="remainingSickLeave">
              <el-input-number
                v-model="form.remainingSickLeave"
                placeholder="请输入剩余病假"
                controls-position="right"
                style="width: 40%;"
              />
              <span style="margin-left: 8px; color: #606266; font-size: 15px;">
                小时 = {{ formatHoursToDays(form.remainingSickLeave) }}
              </span>
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="剩余婚假" prop="remainingMarriageLeave">
              <el-input-number
                v-model="form.remainingMarriageLeave"
                placeholder="请输入剩余婚假"
                controls-position="right"
                style="width: 40%;"
              />
              <span style="margin-left: 8px; color: #606266; font-size: 15px;">
                小时 = {{ formatHoursToDays(form.remainingMarriageLeave) }}
              </span>
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="剩余产假" prop="remainingMaternityLeave">
              <el-input-number
                v-model="form.remainingMaternityLeave"
                placeholder="请输入剩余产假"
                controls-position="right"
                style="width: 40%;"
              />
              <span style="margin-left: 8px; color: #606266; font-size: 15px;">
                小时 = {{ formatHoursToDays(form.remainingMaternityLeave) }}
              </span>
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="剩余陪产假" prop="remainingFaternityLeave">
              <el-input-number
                v-model="form.remainingFaternityLeave"
                placeholder="请输入剩余陪产假"
                controls-position="right"
                style="width: 40%;"
              />
              <span style="margin-left: 8px; color: #606266; font-size: 15px;">
                小时 = {{ formatHoursToDays(form.remainingFaternityLeave) }}
              </span>
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="剩余加班假" prop="remainingOvertimeLeave">
              <el-input-number
                v-model="form.remainingOvertimeLeave"
                placeholder="请输入剩余加班假"
                controls-position="right"
                style="width: 40%;"
              />
              <span style="margin-left: 8px; color: #606266; font-size: 15px;">
                小时 = {{ formatHoursToDays(form.remainingOvertimeLeave) }}
              </span>
            </el-form-item>
      </el-col>
      <el-col :span="8">
            <el-form-item label="剩余其他假" prop="remainingOthersLeave">
              <el-input-number
                v-model="form.remainingOthersLeave"
                placeholder="请输入剩余其他假"
                controls-position="right"
                style="width: 40%;"
              />
              <span style="margin-left: 8px; color: #606266; font-size: 15px;">
                小时 = {{ formatHoursToDays(form.remainingOthersLeave) }}
              </span>
            </el-form-item>
      </el-col>
<!--      <el-col :span="8">-->
<!--            <el-form-item label="用户ID" prop="userId">-->
<!--              <el-input v-model="form.userId"  placeholder="请输入用户ID" />-->
<!--            </el-form-item>-->
<!--      </el-col>-->
<!--      <el-col :span="8">-->
<!--            <el-form-item label="部门ID" prop="deptId">-->
<!--              <el-input v-model="form.deptId"  placeholder="请输入部门ID" />-->
<!--            </el-form-item>-->
<!--      </el-col>-->
<!--      <el-col :span="8">-->
<!--            <el-form-item label="删除标识" prop="delFlag">-->
<!--              <el-input v-model="form.delFlag"  placeholder="请输入删除标识" />-->
<!--            </el-form-item>-->
<!--      </el-col>-->
        </el-row>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 员工假期管理详情对话框 -->
    <el-dialog :title="title" :visible.sync="openInfo" width="68%" append-to-body>
      <el-descriptions border >
                        <el-descriptions-item label="姓名">
                          {{ getEmployeeNameById(form.employeeId) }}
                        </el-descriptions-item>
                        <el-descriptions-item label="工作年限" >{{form.workYear}}</el-descriptions-item>
                        <el-descriptions-item label="年假总时长">
                          {{ formatLeaveHours(form.annualLeave) }}
                        </el-descriptions-item>
                        <el-descriptions-item label="剩余年假">
                          {{ formatLeaveHours(form.remainingAnnualLeave) }}
                        </el-descriptions-item>
                        <el-descriptions-item label="剩余事假" >
                          {{formatLeaveHours(form.remainingPersonalLeave)}}
                        </el-descriptions-item>
                        <el-descriptions-item label="剩余病假" >
                          {{formatLeaveHours(form.remainingSickLeave)}}
                        </el-descriptions-item>
                        <el-descriptions-item label="剩余婚假" >
                          {{formatLeaveHours(form.remainingMarriageLeave)}}
                        </el-descriptions-item>
                        <el-descriptions-item label="剩余产假" >
                          {{formatLeaveHours(form.remainingMaternityLeave)}}
                        </el-descriptions-item>
                        <el-descriptions-item label="剩余陪产假" >
                          {{formatLeaveHours(form.remainingFaternityLeave)}}
                        </el-descriptions-item>
                        <el-descriptions-item label="剩余加班假" >
                          {{formatLeaveHours(form.remainingOvertimeLeave)}}
                        </el-descriptions-item>
                        <el-descriptions-item label="剩余其他假" >
                          {{formatLeaveHours(form.remainingOthersLeave)}}
                        </el-descriptions-item>
<!--                        <el-descriptions-item label="用户ID" >{{form.userId}}</el-descriptions-item>-->
<!--                        <el-descriptions-item label="部门ID" >{{form.deptId}}</el-descriptions-item>-->
<!--                        <el-descriptions-item label="删除标识" >{{form.delFlag}}</el-descriptions-item>-->
      </el-descriptions>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listBalance, getBalance, delBalance, addBalance, updateBalance } from "@/api/leave/balance";
import {listEmployee, getEmployeeByUserId} from "@/api/hrm/employee";

export default {
  name: "Balance",
  data() {
    return {
      employeeOptions: [], // 用于存储员工下拉选项
      // 是否显示更多
      isShowMore: false,
      // el-table 高度
      tableHeight: 650, // 用于存储 el-table 的高度
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 员工假期管理表格数据
      balanceList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否显示详情弹出层
      openInfo: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        employeeId: null,
        workYear: null,
        annualLeave: null,
        remainingAnnualLeave: null,
        remainingPersonalLeave: null,
        remainingSickLeave: null,
        remainingMarriageLeave: null,
        remainingMaternityLeave: null,
        remainingFaternityLeave: null,
        remainingOvertimeLeave: null,
        remainingOthersLeave: null,
        userId: null,
        deptId: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        employeeId: [
          { required: true, message: "姓名不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.loadEmployees();
    this.initCurrentUser();
    this.getList();
    this.setTableHeight();
    window.addEventListener('resize', this.setTableHeight); // 监听窗口大小变化
  },
  beforeDestroy() {
    window.removeEventListener('resize', this.setTableHeight); // 移除事件监听
  },
  computed: {
    isAdmin() {
      return this.$store.getters.roles.includes('admin'); // 或 '超级管理员' 等
    },
    userId() {
      return this.$store.getters.userId; // 或 this.$store.state.user.id
    }
  },
  watch: {
    'form.employeeId'(newVal) {
      if (newVal) {
        const emp = this.employeeOptions.find(e => e.value === newVal);
        let workYear = 0;
        if (emp && emp.probationEnd) {
          const probDate = new Date(emp.probationEnd); // 试用期结束日
          const today = new Date();

          let years = today.getFullYear() - probDate.getFullYear();
          const monthDiff = today.getMonth() - probDate.getMonth();
          const dayDiff = today.getDate() - probDate.getDate();

          // 如果还没到对应月日，则减1年
          if (monthDiff < 0 || (monthDiff === 0 && dayDiff < 0)) {
            years--;
          }
          workYear = Math.max(0, years);
        }

        // 设置工作年限
        this.form.workYear = workYear;

        // 根据工作年限自动计算年假总时长
        let annualLeaveDays = 0;
        if (workYear < 1) {
          annualLeaveDays = 0;
        } else if (workYear >= 1 && workYear < 3) {
          annualLeaveDays = 2;
        } else {
          annualLeaveDays = 5;
        }
        this.form.annualLeave = annualLeaveDays * 8; // 存储为小时
      } else {
        this.form.workYear = 0;
        this.form.annualLeave = 0;
      }
    }
  },
  methods: {
    formatHoursToDays(hours) {
      if (hours == null || hours === '' || isNaN(hours)) {
        return '0天0小时';
      }

      const total = parseFloat(hours);
      if (total === 0) {
        return '0天0小时';
      }

      const isNegative = total < 0;
      const absTotal = Math.abs(total);

      // 对绝对值进行换算
      const days = Math.floor(absTotal / 8);
      const remainingHours = absTotal % 8;

      // 格式化小时部分（整数不显示小数）
      const hrStr = remainingHours % 1 === 0
        ? String(remainingHours)
        : remainingHours.toFixed(1);

      // 拼接结果
      const result = `${days}天${hrStr}小时`;

      // 如果原数为负，整体加负号
      return isNegative ? `-${result}` : result;
    },
    initCurrentUser() {
      if (!this.isAdmin) {
        const userId = this.userId;
        if (!userId) {
          this.$message.error('用户信息缺失');
          return;
        }
        getEmployeeByUserId(userId)
          .then(response => {
            const empId = response.data; // 后端返回的是 empId（即员工ID）
            if (empId) {
              this.queryParams.employeeId = empId;
              // 可选：隐藏员工选择框（因为非管理员只能看自己）
            } else {
              this.$message.warning("您尚未绑定员工信息，无法查看假期数据");
              // 可考虑清空列表或跳转
            }
          })
          .catch(error => {
            console.error("获取员工ID失败", error);
            this.$message.error("获取个人信息失败，请联系管理员");
          });
      }
    },
    formatLeaveHours(hours) {
      if (hours == null || hours === '') return '';
      const total = parseFloat(hours);
      if (isNaN(total)) return '';
      const days = Math.floor(total / 8);
      const hrs = total % 8;
      const hrStr = hrs % 1 === 0 ? String(hrs) : hrs.toFixed(1);
      return `${days}天${hrStr}小时`;
    },
    getEmployeeNameById(id) {
      const emp = this.employeeOptions.find(e => e.value === id);
      return emp ? emp.label : '未知';
    },
    loadEmployees() {
      // 有一个 API返回 { id, name } 列表
      listEmployee({ pageNum: 1, pageSize: 100 }).then(response => {
        this.employeeOptions = response.rows.map(emp => ({
          value: emp.empId,        // 实际存储的是员工ID（更合理）
          label: emp.empName,      // 显示的是姓名
          probationEnd: emp.probationEnd // 试用期结束时间
        }));
      });
    },
    /** 查询员工假期管理列表 */
    getList() {
      this.loading = true;
      listBalance(this.queryParams).then(response => {
        this.balanceList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.openInfo = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        employeeId: null,
        workYear: null,
        annualLeave: null,
        remainingAnnualLeave: null,
        remainingPersonalLeave: null,
        remainingSickLeave: null,
        remainingMarriageLeave: null,
        remainingMaternityLeave: null,
        remainingFaternityLeave: null,
        remainingOvertimeLeave: null,
        remainingOthersLeave: null,
        createTime: null,
        updateTime: null,
        userId: null,
        deptId: null,
        createBy: null,
        updateBy: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加员工假期管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getBalance(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改员工假期管理";
      });
    },
    /** 详情按钮操作 */
    handleInfo(row) {
      this.reset();
      const id = row.id || this.ids
      getBalance(id).then(response => {
        this.form = response.data;
        this.openInfo = true;
        this.title = "员工假期管理详情";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateBalance(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBalance(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除员工假期管理编号为"' + ids + '"的数据项？').then(function() {
        return delBalance(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('leave/balance/export', {
        ...this.queryParams
      }, `员工假期管理数据导出_${new Date().getTime()}.xlsx`)
    },
    // 设置表格高度
    setTableHeight() {
      const appContainer = document.getElementById('app-container');
      if (appContainer) {
        this.tableHeight = appContainer.clientHeight * 0.8;
      }
    }
  }
};
</script>
